package com.archstoday.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.archstoday.R;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 微信工具
 */
public class WechatUtils {

    public static final String APP_ID = "wxa54b5d44c255266b";
    private static IWXAPI api;

    public static void regToWx(Context context) {
        api = WXAPIFactory.createWXAPI(context, APP_ID, true);

        api.registerApp(APP_ID);
    }

    public static void share(Context context, String title, String des, String url, boolean isGroup) {
        // 初始化一个WXWebpageObject对象，填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;

        // 用WXWebpageObject对象初始化一个WXMediaMessage对象，填写标题，描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = des;
        Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(),
                R.mipmap.ic_launcher);
//        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
//        bmp.recycle();
        msg.thumbData = bmpToByteArray(thumbBmp, true);

        // 构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("share"); // transaction用于唯一标识一个请求
        req.message = msg;
        req.scene = isGroup ?
                SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;

        // 调用api接口发送数据到微信
        api.sendReq(req);
    }

    private static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }


    public static void shareBigThumb(Context mContext, Bitmap bitmap, int to) {
        if (bitmap == null) {
            return;
        }

        //第二步：创建一个WXImageObject对象，用来包装bitmap对象
        WXImageObject imageObject = new WXImageObject(bitmap);
        //第三步：创建对象WXMediaMessage对象，并包装这个对像imageObject
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imageObject;

        //第四步压缩图像
        Bitmap thBitmap = Bitmap.createScaledBitmap(bitmap, 120, 150, true);
        bitmap.recycle();//释放图像所占用的图片内存资源
        msg.thumbData = bmpByteArray(thBitmap, true);//设置缩略图
        //发送是缩略图，显示是原图
        //第五步 ：创建SendMessageToWX.Req对象，用于发送数据
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        //生成请求唯一标识
        req.transaction = buildTransaction("img");
        req.message = msg;

        req.scene = SendMessageToWX.Req.WXSceneSession;
        if (to == 1) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        }

        if (to == 2) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }

        //第六步:发送给客户端
        boolean sendState = api.sendReq(req);//发送成功返回true，否则返回false
        if (sendState) {
            Toast.makeText(mContext, "分享二进制图像成功", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(mContext, "分享二进制图像失败", Toast.LENGTH_LONG).show();
        }
    }

    private static byte[] bmpByteArray(final Bitmap bitmap, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();//定义一个字节数组输出流对象，用于接收转换后二进制的结果
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);//把二进制的结果放在output对象中
        if (needRecycle) {
            bitmap.recycle();//释放图片资源的内存
        }
        byte[] result = output.toByteArray();//把output对象转化成实际的数组
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}